﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WMP
{
    class MediaPlayer
    {

        private PlayLists_OUTDATED playList = new PlayLists_OUTDATED();
        private LecteurA lecteurA = new LecteurA();
        private LecteurV lecteurV = new LecteurV();
        private PlayLists_OUTDATED.Type current = PlayLists_OUTDATED.Type.EOL;

        private State playA(PlayLists_OUTDATED.Item item)
        {
            if (lecteurA.getState() == State.PAUSE ||
                lecteurA.getState() == State.STOP)
            {
                lecteurA.playsound();
                return (State.PLAY);
            }
            else if (lecteurA.getState() == State.ENDED)
            {
                if (item.type == PlayLists_OUTDATED.Type.MP3)
                {
                    lecteurA.playsound(item.name);
                    current = PlayLists_OUTDATED.Type.MP3;
                    return (State.PLAY);
                }
                else
                    return (State.NOTHING);
            }
            else
            {
                lecteurA.pause();
                return (State.STOP);
            }
        }

        private State playV(PlayLists_OUTDATED.Item item)
        {
            if (lecteurV.getState() == State.PAUSE ||
                lecteurV.getState() == State.STOP)
            {
                lecteurV.playsound();
                return (State.PLAY);
            }
            else if (lecteurV.getState() == State.ENDED)
            {
                if (item.type == PlayLists_OUTDATED.Type.MP3)
                {
                    lecteurV.playsound(item.name);
                    return (State.PLAY);
                }
                else
                    return (State.NOTHING);
            }
            else
            {
                lecteurV.pause();
                return (State.PAUSE);
            }
        }

        private State avanceA(PlayLists_OUTDATED.Item item)
        {
            stopA();
            if (item.type == PlayLists_OUTDATED.Type.MP3)
            {
                lecteurA.playsound(item.name);
                current = PlayLists_OUTDATED.Type.MP3;
                return (State.PLAY);
            }
            return (State.NOTHING);    
        }

        private State avanceV(PlayLists_OUTDATED.Item item)
        {
            stopV();
            if (item.type == PlayLists_OUTDATED.Type.MPG)
            {
                lecteurV.playsound(item.name);
                current = PlayLists_OUTDATED.Type.MPG;
                return (State.PLAY);
            }
            return (State.NOTHING);
        }

        private State stopA()
        {
            if (lecteurA.getState() != State.STOP ||
                lecteurA.getState() != State.ENDED)
            {
                lecteurA.stop();
                return (State.STOP);
            }
            return (State.NOTHING);
        }

        private State stopV()
        {
            if (lecteurV.getState() != State.STOP ||
                lecteurV.getState() != State.ENDED)
            {
                lecteurV.stop();
                return (State.STOP);
            }
            return(State.NOTHING);
        }

        private State reculeA(PlayLists_OUTDATED.Item item)
        {
            if (item.type == PlayLists_OUTDATED.Type.MP3)
            {
                lecteurA.playsound(item.name);
                current = PlayLists_OUTDATED.Type.MP3;
                return (State.PLAY);
            }
            return (State.NOTHING);
        }

        private State reculeV(PlayLists_OUTDATED.Item item)
        {
            if (item.type == PlayLists_OUTDATED.Type.MPG)
            {
                lecteurV.playsound(item.name);
                current = PlayLists_OUTDATED.Type.MPG;
                return (State.PLAY);
            }
            return (State.NOTHING);
        }

        public State getLecteurState()
        {
            return (State.ENDED);
        }

        /*public string add()
        {
            System.Windows.Forms.OpenFileDialog file = new System.Windows.Forms.OpenFileDialog();
            if (file.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
            {
                
                playList.addItem(file.FileName, PlayList.Type.MP3);

                return (file.FileName);
            }
            return ("CANCEL");
        }*/

        public State play()
        {
            PlayLists_OUTDATED.Item item = new PlayLists_OUTDATED.Item();
            if (lecteurA.getState() == State.ENDED &&
                lecteurV.getState() == State.ENDED)
            {
                item = playList.getNextItem();
                if (playA(item) == State.NOTHING)
                    return (playV(item));
            }
            else if (current == PlayLists_OUTDATED.Type.MP3)
                return (playA(item));
            else if (current == PlayLists_OUTDATED.Type.MPG)
                return (playV(item));
            return (State.NOTHING);
        }

        public State avance()
        {
            stop();
            PlayLists_OUTDATED.Item item = playList.getNextItem();
            if (avanceA(item) == State.PLAY || avanceV(item) == State.PLAY)
                return (State.PLAY);
            return (State.NOTHING);
        }

        public State stop()
        {
            if (current == PlayLists_OUTDATED.Type.MP3)
                return (stopA());
            else if (current == PlayLists_OUTDATED.Type.MPG)
                return (stopV());
            else
                return (State.NOTHING);
        }

        public State recule()
        {
            stop();
            PlayLists_OUTDATED.Item item = playList.getPrevItem();
            if (reculeA(item) == State.PLAY || reculeV(item) == State.PLAY)
                return (State.PLAY);
            return (State.NOTHING);    
        }
    }
}